<template>
	<view class="item-box">
		<view class="item-top">
			<view class="item-title">
				{{getTitle()}}
			</view>
			<view class="item-btn" v-if="appeal" @click="$emit('appeal')">
				申诉
			</view>
			<view class="item-btn" v-if="detail" @click="$emit('detail')">
				查看
			</view>
		</view>
		<view class="item-bottom">
			<view class="item-text">
				<view class="item-field" v-for="(item,i) in o.list" :key="i">
					<view class="field-label">
						{{item.label}}：
					</view>
					<view class="field-value">
						{{getContent(item)}}
					</view>
				</view>
			</view>
			<view class="item-num" :style="itemNumStyle">
				{{itemNum}}
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		props: {
			items: {
				type: Object,
				default () {
					return {
						deductPoint: "0",
						itemCode: "",
					}
				}
			},
			appeal: Boolean,
			detail: Boolean,
		},
		data() {
			return {
				options: {
					pljs: {
						title: "vehCode",
						list: [{
								label: '发生地点',
								prop: 'occurAddr',
							},
							{
								label: '发生时间',
								prop: 'evalTime',
							}
						]
					},
					ybcs: {
						title: "vehCode",
						list: [{
								label: '超速地点',
								prop: 'occurAddr',
							},
							{
								label: '超速时间',
								prop: 'evalTime',
							}
						]
					},
					ydztczbgf: {
						title: 'billNo',
						list: [{
								label: '发生时间',
								prop: 'evalTime'
							},
							{
								label: '描述',
								prop: 'description'
							}
						]
					},
					jjwfsjkf: {
						title: '',
						list: [{
								label: '发生时间',
								prop: 'evalTime'
							},
							{
								label: '案由',
								prop: 'cause'
							}
						]
					},
					yzcf: {
						title: 'vehCode',
						list: [{
								label: '发生时间',
								prop: 'evalTime'
							},
							{
								label: '案由',
								prop: 'cause'
							}
						]
					},
				}
			}
		},
		computed: {
			o() {
				if (this.items.itemCode) {
					return this.options[this.items.itemCode]
				} else {
					return {
						title: "",
						list: []
					}
				}
			},
			itemNumStyle() {
				let res = {}
				if (this.items.deductPoint == '0') {
					res.color = '#333333';
				} else if (this.items.deductPoint.startsWith('-')) {
					res.color = '#d60a0a';
				} else {
					res.color = '#388101';
				}
				return res;
			},
			itemNum() {
				let res = Number(this.items.deductPoint)
				if (res > 0) {
					res = "+" + res;
				}
				return res;
			},
		},
		methods: {
			getTitle() {
				let res = this.o.title ? this.items[this.o.title] || '-' : ''
				if (this.items.itemCode == 'yzcf') {
					if (this.items.vehColor) {
						res += '(' + this.items.vehColor + ')'
					}
				}
				return res;
			},
			getContent(item) {
				return this.items[item.prop];
			},
		},
		mounted() {
			if (this.items.occurAddr) {
				this.$api.getAddress(this.items.occurAddr).then(res => {
					this.items.occurAddr = res.data.regeocode.formatted_address || '-';
				})
			} else {
				this.items.occurAddr = '-'
			}
		}
	}
</script>

<style lang="scss" scope>
	.item-box {
		background: #fff;
		border-radius: 8rpx;
		padding: 10rpx 20rpx;
		box-shadow: 0px 4rpx 6rpx 1px rgba(84, 83, 83, 0.16);

		.item-top {
			display: flex;
			border-bottom: 1rpx solid #ddd;
			padding: 0 10rpx;
			height: 70rpx;
			align-items: center;

			.item-title {
				flex: 1;
				font-weight: bold;
				font-size: 32rpx;
			}

			.item-btn {
				background: #f2ba38;
				color: #fff;
				padding: 0 0.8em;
				border-radius: 8rpx;
				font-size: 24rpx;
				line-height: 46rpx;
			}
		}

		.item-bottom {
			display: flex;
			padding: 10rpx;
			align-items: center;

			.item-text {
				flex: 1;

				.item-field {
					display: flex;
					align-items: flex-start;
					font-size: 28rpx;
					line-height: 1.4em;
					padding: 10rpx 0;

					.field-label {
						width: 5em;
					}

					.field-value {
						flex: 1;
					}
				}
			}

			.item-num {
				padding: 0 20rpx;
			}
		}
	}
</style>